package com.yykj.im.center.server.task;

import com.alibaba.fastjson.JSON;
import com.yykj.im.center.client.dto.UserInfoDto;
import com.yykj.im.center.server.entity.Notice;
import com.yykj.im.center.server.enums.CustomType;
import com.yykj.im.center.server.repository.NoticeMapper;
import com.yykj.im.center.server.util.ConvertUtil;
import com.yykj.im.center.server.util.LocalDateTimeUtil;
import com.yykj.tencent.im.client.feign.TencentImFeignClient;
import com.yykj.tencent.im.client.form.BatchSendSystemMsgRequest;
import com.yykj.tencent.im.client.form.SendMsgResult;
import com.yykj.tencent.im.client.form.TpnsSendForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@Component
@Slf4j
public class SchedulerTask {

/*//    @Async
    @Scheduled(cron = "0 30 1 * * ?")
    //夜里1点半执行
    public void deleteImage() {
        log.info("deleteImage执行");
    }
  */

    @Autowired
    NoticeMapper noticeMapper;

    @Autowired
    TencentImFeignClient tencentImFeignClient;

    @Scheduled(fixedRate = 60000)
    public void send() {
        log.info("send message scheduler");
        List<Notice> notices = this.noticeMapper.listNeedSend();
        if (notices != null && notices.size() > 0) {
            for (Notice notice : notices) {
                ArrayList<String> accounts = new ArrayList<>();
                HashSet<String> accountLists = new HashSet<>();
                ArrayList<UserInfoDto> userInfo = ConvertUtil.getUserInfo(notice.getUsers());
                for (UserInfoDto user : userInfo) {
                    accounts.add(user.getTencentAccountId());
                    accountLists.add(user.getTencentAccountId());
                }

                this.tencentImFeignClient.tpnsSend(TpnsSendForm.builder()
                        .accountList(accountLists)
                        .title(notice.getTitle())
                        .content("查看详情").build());
                HashMap<String, Object> msgBody = new HashMap<>();
                msgBody.put("type", CustomType.systemNotice);
                HashMap<String, Object> data = new HashMap<>();
                data.put("title", notice.getTitle());
                data.put("content", notice.getContent());
                data.put("sendTime", LocalDateTimeUtil.LocalDateTime2String(notice.getSendTime()));
                data.put("url", notice.getUrl());
                data.put("remark", notice.getRemark());
                msgBody.put("data", data);
                BatchSendSystemMsgRequest batchSendSystemMsgRequest = BatchSendSystemMsgRequest.builder()
                        .toAccounts(accounts)
                        .msgBody(JSON.toJSONString(msgBody)).build();
                SendMsgResult sendMsgResult = this.tencentImFeignClient.batchSendCustomMsg(batchSendSystemMsgRequest);
                if (sendMsgResult != null && StringUtils.isNotBlank(sendMsgResult.getMsgKey())) {
                    notice.setStatus(1);
                    notice.setDoSendTime(LocalDateTimeUtil.now());
                    notice.setMsgKey(sendMsgResult.getMsgKey());
                } else {
                    notice.setStatus(2);
                }
                this.noticeMapper.updateById(notice);
            }
        }
    }


}
